package wan.com.templateproject.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wan.com.templateproject.common.BaseResponse;
import wan.com.templateproject.entity.People;
import wan.com.templateproject.mapper.PeopleMapper;
import wan.com.templateproject.service.PeopleService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

/**
 * @author wan
 * @version 1.0.0
 * @Description
 * @createTime 2021年10月11日 13:46:07
 */
@Slf4j
@RestController
@RequestMapping("/people")
public class PeopleController {

    @Autowired
    private PeopleService peopleService;

    @Resource
    private PeopleMapper peopleMapper;

    @ApiOperation("多线程发送redis")
    @RequestMapping(value = "/send", method = RequestMethod.GET)
    @ApiImplicitParam(name = "num", value = "发送多少条", type = "Integer", required = false)
    public BaseResponse<Boolean> sendRedis(@RequestParam Integer num) throws InterruptedException {
        return peopleService.sendRedis(num);
    }


    @ApiOperation("增加people信息")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResponse<Integer> addPeople() {
//        return peopleService.updatePeople(new People());
//        People people = People.builder().name("梁亚泽")
//                .age(25)
//                .gender(1)
//                .identityNum("2022")
//                .build();
        People people = null;
        int result = peopleMapper.addPeople(people);
        if (result > 0) {
            return new BaseResponse<>(1);
        }
        return new BaseResponse<>(0);
    }

    @ApiOperation("修改people信息")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResponse<Integer> updatePeople() {
//        People people = People.builder().name("梁亚泽")
//                .age(25)
//                .gender(0)
//                .identityNum("2022")
//                .build();
        People people = null;
        int result = peopleMapper.updatePeople(people);
        if (result > 0) {
            return new BaseResponse<>(1);
        }
        return new BaseResponse<>(0);
    }

    @RequestMapping(value = "rabbit", method = RequestMethod.GET)
    public BaseResponse<Boolean> sendRabbitmq(@RequestParam("msg") String msg) {
        return peopleService.sendRabbitmq(msg);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public BaseResponse<PageInfo<People>> getPeoples(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        return peopleService.getPeoples(pageNum, pageSize);
    }

    @Autowired
    HttpServletResponse response;

    @SneakyThrows
    @GetMapping("/excel/out")
    public void excelOut() {
        XSSFWorkbook xWork = new XSSFWorkbook();
        XSSFSheet sheet = xWork.createSheet("Sheet1");
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        XSSFCell cell1 = row.createCell(1);
        cell.setCellValue("第一行，第一列");
        cell1.setCellValue("第一行，第二列");
        //使用该头信息，意味着不想直接显示内容
//        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        //attachment为以附件方式下载
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(DateUtil.parse(DateUtil.date().toString(), DatePattern.NORM_DATETIME_MINUTE_PATTERN) + ".xlsx", "utf-8"));
        /**
         * 代码里面使用Content-Disposition来确保浏览器弹出下载对话框的时候。
         * response.addHeader("Content-Disposition","attachment");一定要确保没有做过关于禁止浏览器缓存的操作
         */
//        response.setHeader("Cache-Control", "No-cache");
        response.flushBuffer();
        xWork.write(response.getOutputStream());
        xWork.close();
    }
}
